<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/1/11 0011
 * Time: 15:02
 */

namespace app\index\model;


use think\Model;

class Brand extends Model
{
    protected $error;//受保护的报错误方法
    protected $pk = 'brand_id';

    public function getError(){
        return $this->error;}


    //模糊查询
    public function searchBrandNameAttr($query,$value,$data){
        if(!empty($value)){
            $query->whereLike('brand_name','%'.$value.'%');
        }
    }
    public function searchRecommendedAttr($query,$value,$data){
        if('2' !== $value){
            $query->whereIn('recommended',$value);
        }
    }
    public function searchBrandLogoAttr($query,$value,$data){
        if ( is_array($value) && 1 == count($value) && '1'==$value[0]){
            $query->whereNotNull('brand_logo');
        }
        if (is_array($value) && 1 == count($value) && '2'==$value[0]){
            $query->whereNull('brand_logo');
        }
    }


    public static function getBrand($data=[],$query=[])
    {
        $field=[
            "if(recommended=1,'推荐','不推荐')" => 'recommended',
            "if(if_show=1,'是','否')" => 'show','if_show',
            "brand_id","brand_name","sort_order","brand_logo","tag"
        ];
        $model = self::field($field)
                ->withSearch(array_keys($data),$data)
                ->order('sort_order');
        // 分页
        $result = $model->paginate([
            'list_rows' => 10,
            'query'     => $query,
        ])->each(function($item) use($data){
            if(isset($data['brand_name']) && ''!=$data['brand_name']){
                $item['brand_name']=
                    str_replace($data['brand_name'],
                        '<span style="color: red;font-weight: 900">'.$data['brand_name'].'</span>',$item['brand_name']);
                return $item;
            }
        });

        return $result;
    }

    public function brandAdd(array $data, array $allowField = [], bool $replace = false)
    {
        try{
            if(empty($data)){
                $this->error = '新增数据不能为空！';
                return ;
            }
            // 限制写入的字段列表
            if (!empty($allowField))
                $this->allowField($allowField);
            $this->replace($replace)->save($data);

            return true;
        }catch (Exception $ex){
            $this->error = $ex->getMessage();
            return false;
        }
    }
    public function getInsertId(){// 获取id的方法
        $data = $this->toArray();
        return $data[$this->pk];
    }

    // 修改
    public function changetrytwo($data, $where = [], $allowField = [])
    {
        try{
            if (empty($data)){
                $this->error = "修改数据不能为空!!";
                return;
            }
            if (!empty($allowField)){
                $this->allowField($allowField);

            }
            if (!empty($where)){
                $this->setUpdateWhere($where);
                if (isset($data[$this->pk]))  unset($data[$this->pk]);
            }
            return $this->exists(true)->save($data);

        }catch (Exception $e){
            $this->error = $e->getMessage();
            return false;
        }
    }
    
}